import 'dart:ui';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/widget/icon_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'dart:io';

class AppWidget {
  AppWidget._();

  static Widget genderAndAge(int gender, int age) {
    return Container(
      height: 16,
      padding: const EdgeInsets.symmetric(horizontal: 6),
      decoration: ShapeDecoration(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(8),
          ),
          color: gender == 0 ? ThemeColor.mainColor : Colors.blueAccent),
      alignment: Alignment.center,
      child: IconText(
        "$age",
        style: const TextStyle(
          color: Colors.white,
          fontSize: 10,
        ),
        icon: Icon(
          gender == 0 ? Icons.female : Icons.male,
          size: 10,
          color: Colors.white,
        ),
      ),
    );
  }

  static PreferredSizeWidget statusBar() {
    return PreferredSize(
      preferredSize:
          Size.fromHeight(MediaQueryData.fromWindow(window).padding.top),
      child: const SafeArea(
        top: true,
        child: Offstage(),
      ),
    );
  }

  static PreferredSizeWidget titleBar(String title, {double elevation = 0,Color? backgroundColor,List<Widget>? actions}) {
    return AppBar(
      title: Text(
        title,
        style: const TextStyle(color: Colors.black, fontSize: 16),
      ),
      elevation: elevation,
      centerTitle: true,
      backgroundColor: backgroundColor??Colors.white,
      leading: leftArrowWidget('images/map/left_back_arrow.png'),
      actions: actions,
    );
  }

  static navTitleBar(
      {String title = '',
      Widget? childWidget,
      List<Widget>? rightActions,
      Color? bgColor,
      double elevation = 0,
      Color? appBarColor,
      String? iconStr,
      Widget? titleWidget,
      bool isHiddenLeftArrow = false,
      Color? leftIconColor,
      PreferredSizeWidget? bottom,
      Function? backAction}) {
    return Scaffold(
      appBar: AppBar(
        title: titleWidget ??
            Text(
              title,
              style: TextStyle(
                  fontSize: 18,
                  color: ThemeColor.textColor,
                  fontWeight: FontWeight.w500),
            ),
        centerTitle: true,
        elevation: elevation,
        backgroundColor: appBarColor ?? Colors.white,
        actions: rightActions,
        bottom: bottom,
        leading: leftArrowWidget(iconStr,
            isHiddenLeftArrow: isHiddenLeftArrow,
            backAction: backAction,
            leftIconColor: leftIconColor),
      ),
      backgroundColor: bgColor ?? Colors.white,
      body: childWidget,
    );
  }

  static leftArrowWidget(String? iconStr,
      {bool isHiddenLeftArrow = false,
      Function? backAction,
      Color? leftIconColor}) {
    return isHiddenLeftArrow
        ? null
        : UnconstrainedBox(
            child: CupertinoButton(
              padding: EdgeInsets.zero,
              onPressed: () {
                if (backAction != null) {
                  backAction();
                } else {
                  Get.back();
                }
              },
              child: Image.asset(
                iconStr ?? 'images/map/left_back_arrow.png',
                width: 16,
                color: leftIconColor ?? Colors.black,
              ),
            ),
          );
  }

  //修改状态栏
  static setStatusBlackView({Color? bgColor}) {
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
        statusBarColor: bgColor ?? Colors.transparent,
        statusBarIconBrightness: Brightness.dark);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }

  static blurFixImage(
      {String? url,
      double imgWidth = 40,
      int sign = 0,
      double sigmaNum = 2.5}) {
    double radius = 5.0;
    String imgUrl = url ??= '';
    Widget commonImgView = ClipRRect(
      borderRadius: BorderRadius.circular(radius),
      child: CachedNetworkImage(
        imageUrl: imgUrl,
        width: imgWidth,
        height: imgWidth,
        fit: BoxFit.cover,
      ),
    );
    Widget showImgView = sign == 1
        ? ClipRRect(
            borderRadius: BorderRadius.circular(radius),
            child: Stack(
              children: [
                commonImgView,
                BackdropFilter(
                  filter: ImageFilter.blur(sigmaX: sigmaNum, sigmaY: sigmaNum),
                  child: Opacity(
                    opacity: 0.05,
                    child: Container(
                        height: imgWidth,
                        width: imgWidth,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(radius),
                        )
                        // color: const Color.fromARGB(85, 0, 0, 0),
                        ),
                  ),
                ),
              ],
            ),
          )
        : commonImgView;

    return imgUrl.isEmpty ? Container() : showImgView;
  }

  static Widget commonEmptyImg({
    String? url,
    double imgWidth = 0,
    double imgHeight = 0,
    double radius = 5,
  }) {
    url ??= '';
    return url.isEmpty
        ? Container()
        : ClipRRect(
            borderRadius: BorderRadius.circular(radius),
            child: Image.network(
              url,
              width: imgWidth,
              height: imgHeight,
              fit: BoxFit.cover,
            ),
          );
  }

  static getProviderImgView(String imgUrl) {
    print(imgUrl + '选择的照片');
    ImageProvider imageProvider;
    if (imgUrl.contains('http')) {
      imageProvider = NetworkImage(
        imgUrl,
      );
    } else {
      imageProvider = FileImage(File(imgUrl));
    }
    return imageProvider;
  }
}
